package com.ww.mine

import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import com.eyepetizer.android.Const
import com.eyepetizer.android.EyepetizerApplication
import com.eyepetizer.android.ui.common.ui.BaseFragment
import com.eyepetizer.android.ui.common.ui.WebViewActivity
import com.ww.mine.extension.setOnClickListener
import kotlinx.android.synthetic.main.fragment_mine.*
import zeus.plugin.PluginManager

/**
 *
 * @author weiwei
 * Created on 2022/2/8 6:40 下午
 */
class MineFragment : BaseFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
//        return super.onCreateView(inflater.inflate(R.layout.fragment_mine, container, false))

        /**
         * 用上面那种方式加载 不会走到BaseActivity 的getSystemService（）方法  在这个方法中
         * 我们可以参考ContextThemeWrapper的getSystemService()函数 中 的代码 mInflater = LayoutInflater.from(getBaseContext()).cloneInContext(this);
         * 并不是直接使用from()函数的返回值，还要克隆那么一下子。克隆就是重新new一个PhoneLayoutInflater对象，并且将其mContext调整为合适的ContextWrapper。
         * LayoutInflater.from(context) 会调用到我们重写的的getSystemService 会替换成功 不会出错，暂时先这么改，用上面那种方式肯定也可以，暂时还没解决
         *
         * 掌阅是使用coverFragmentManager通过自定义LayoutInflater 并没有使用系统的inflater 而且统一解决深色模式问题  不使用系统的inflater
         * */
        var view = LayoutInflater.from(context).inflate(R.layout.fragment_mine, null)
        return super.onCreateView(view)


    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        tvVersionNumber.text = String.format(
            getActivity()?.applicationContext?.getString(R.string.version_show)
                .toString(), "6.3.01"
        )
        setOnClickListener(
            ivMore,
            ivAvatar,
            tvLoginTips,
            tvFavorites,
            tvCache,
            tvFollow,
            tvWatchRecord,
            tvNotificationToggle,
            tvMyBadge,
            tvFeedback,
            tvContribute,
            tvVersionNumber,
            rootView,
            llScrollViewContent,
            tvTest
        ) {
            when (this) {
//                ivMore -> SettingActivity.start(activity)
//
//                ivAvatar, tvLoginTips, tvFavorites, tvCache, tvFollow, tvWatchRecord, tvNotificationToggle, tvMyBadge -> {
//                    LoginActivity.start(activity)
//                }
                tvContribute -> {
                    Log.d(TAG, "onActivityCreated: tvContribute")

                    WebViewActivity.start(
                        activity,
                        WebViewActivity.DEFAULT_TITLE,
                        Const.Url.AUTHOR_OPEN,
                        false,
                        false
                    )
                }
                tvFeedback -> {
                    Log.d(TAG, "onActivityCreated: tvFeedback")
                    WebViewActivity.start(
                        activity,
                        WebViewActivity.DEFAULT_TITLE,
                        WebViewActivity.DEFAULT_URL,
                        true,
                        false,
                        MODE_SONIC_WITH_OFFLINE_CACHE
                    )
                }
                tvVersionNumber -> {
                    Log.d(TAG, "onActivityCreated: tvVersionNumber")

                    WebViewActivity.start(
                        activity,
                        WebViewActivity.DEFAULT_TITLE,
                        WebViewActivity.DEFAULT_URL,
                        true,
                        false,
                        MODE_SONIC_WITH_OFFLINE_CACHE
                    )
                }
//                tvTest -> {
//                    startActivity(Intent(activity, TestPluginActivity::class.java))
//                }
                this@MineFragment.rootView, llScrollViewContent -> {
                    Log.d(TAG, "onActivityCreated: AboutActivity")
//                    AboutActivity.start(activity)
                    val cl: Class<*> = PluginManager.mNowClassLoader.loadClass("com.ww.mine.AboutActivity")
                    val intent = Intent(EyepetizerApplication.getInstance().baseContext, cl)

                    PluginManager.startActivity(activity,intent)
//                    activity.startActivity(intent)
                }

                else -> {
                }
            }
        }

        tvVersionNumber.setOnLongClickListener {
            Toast.makeText(
                getActivity(),
                getActivity()?.applicationContext?.getString(R.string.build_type).toString(),
                Toast.LENGTH_LONG
            ).show()
//            String.format(getActivity(),getActivity()?.applicationContext?.getString(R.string.build_type).toString(), BuildConfig.BUILD_TYPE).showToast()
            true
        }

    }

    companion object {
        const val MODE_SONIC_WITH_OFFLINE_CACHE = 2
        fun newInstance() = MineFragment()
    }
}